package gs.gui;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;

public class AdminWindow extends JFrame {
	
	JTabbedPane editPane;
	JTabbedPane tpane;
	JTabbedPane statPane;
	
	AccountPanel account;
	CharacterPanel character;
	ItemPanel item;
	ItemStatPanel itemstat;
	NPCPanel npc;
	NPCStatPanel npcstat;
	NPCLocationPanel location;
	QuestPanel quest;
	QuestStatPanel queststat;
	SpellPanel spell;
	SpellStatPanel spellstat;
	
	ConnectionController connection;
	JLabel messageLabel;
	
	public void initUI() {
		this.setLayout(new BorderLayout());
		
		setTitle("Administracja");
		setSize(450, 350);
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);    
		
		ChangeListener ch = new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e)
			{
				refreshTab();
			}
		};
		
		tpane = new JTabbedPane();
		tpane.addChangeListener(ch);
		statPane = new JTabbedPane();	
		statPane.addChangeListener(ch);
		editPane = new JTabbedPane();
		editPane.addChangeListener(ch);
		
		account = new AccountPanel(connection, this);
		character =  new CharacterPanel(connection,this);
		item =  new ItemPanel(connection,this);
		spell =  new SpellPanel(connection,this);
		npc = new NPCPanel(connection,this);
		quest =  new QuestPanel(connection,this);
		location =  new NPCLocationPanel(connection,this);
		
		itemstat = new ItemStatPanel(connection,this); 
		spellstat =  new SpellStatPanel(connection,this);
		npcstat = new NPCStatPanel(connection,this);
		queststat = new QuestStatPanel(connection,this);
		
		editPane.addTab("Konta", account);
		editPane.addTab("Postaci", character);
		editPane.addTab("Przedmioty", item);
		editPane.addTab("Czary", spell);
		editPane.addTab("NPC", npc);
		editPane.addTab("Zadania", quest);
		editPane.addTab("Lokacje", location);
		
		statPane.addTab("Przedmioty", itemstat);
		statPane.addTab("Czary", spellstat);
		statPane.addTab("NPC", npcstat);
		statPane.addTab("Zadania", queststat);
		
		tpane.addTab("Edycja", editPane);
		tpane.addTab("Statystyki", statPane);
		
		messageLabel = new JLabel(" ");
		
		getContentPane().add(tpane, BorderLayout.CENTER);
		getContentPane().add(messageLabel,BorderLayout.SOUTH);
	
	}
	
	public void refreshTab()
	{
		if(tpane.getSelectedComponent() == editPane)
		{
			switch(editPane.getSelectedIndex())
			{
				case 0: account.refresh(); return;
				case 1: character.refresh(); return;
				case 2: item.refresh(); return;
				case 3: spell.refresh(); return;
				case 4: npc.refresh(); return;
				case 5: quest.refresh(); return;
			}
		} else
		{
			switch(statPane.getSelectedIndex())
			{
				case 0: itemstat.refresh(); return;
				case 1: spellstat.refresh(); return;
				case 2: npcstat.refresh(); return;
				case 3: queststat.refresh(); return;
			}
		}
	}
	
	public void showError(String error)
	{
		messageLabel.setText(error);
		messageLabel.setForeground(Color.red);
	}
	
	public void showSuccess(String mes)
	{
		messageLabel.setText(mes);
		messageLabel.setForeground(new Color(0,100,0));
	}
	
	public void showInfo(String mes)
	{
		messageLabel.setText(mes);
		messageLabel.setForeground(Color.black);
	}
	
	AdminWindow(ConnectionController c)
	{
		connection = c;
	}
	
}
